package com.hooya.fa.eu.biz.dao.manager.special;

import com.hooya.fa.eu.api.bo.PurchaseBO;
import com.hooya.fa.eu.api.bo.SkuLastPriceBO;
import com.hooya.fa.eu.biz.dao.mapper.MetaRelationMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Description 1
 * @Author : Zongz
 * @Copyright Copyright (c) 豪雅集团
 * @Create 2025/2/19
 * @Version 1.1.0
 */
@Slf4j
@Component
@AllArgsConstructor
public class TradeRecordCgManager {

    private final MetaRelationMapper metaRelationMapper;

    /**
     * 补充采购信息
     *
     * @param purchaseList 采购列表
     */
    public void suppleInformation(List<PurchaseBO> purchaseList) {
        List<PurchaseBO> noPriceList = purchaseList.stream().filter(l -> Double.isNaN(l.getPrice()))
                .collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(noPriceList)) {
            Set<String> skuList = noPriceList.stream().map(PurchaseBO::getCpbh).collect(Collectors.toSet());
            List<SkuLastPriceBO> lastPriceList = metaRelationMapper.getSkuLastPriceByBelongs(skuList);
            if (CollectionUtils.isEmpty(lastPriceList)) {
                throw new RuntimeException("以下找不到sku价格信息：" + lastPriceList);
            }
            Map<String, SkuLastPriceBO> priceMap = lastPriceList.stream()
                    .collect(Collectors.toMap(l -> l.getSku() + l.getBelongs(), i -> i));
            for (PurchaseBO bo : noPriceList) {
                String key = bo.getCpbh() + bo.getBelongs();
                SkuLastPriceBO skuData = priceMap.get(key);
                if (Objects.nonNull(skuData)) {
                    purchaseList.stream().filter(l -> l.getSheetNumber().equals(bo.getSheetNumber())).forEach(l -> {
                        l.setPrice(skuData.getPrice());
                        l.setInvoiceDate(skuData.getInvoiceDate());
                        l.setCurrency(skuData.getCurrency());
                    });
                } else {
                    String newKey = bo.getCpbh() + (bo.getBelongs().split("-")[0].equals("costway") ?
                            "fds" : "costway" + bo.getBelongs().split("-")[1]);
                    SkuLastPriceBO skuData2 = priceMap.get(newKey);
                    if (Objects.nonNull(skuData2)) {
                        purchaseList.stream().filter(l -> l.getSheetNumber().equals(bo.getSheetNumber())).forEach(l -> {
                            l.setPrice(skuData2.getPrice());
                            l.setInvoiceDate(skuData2.getInvoiceDate());
                            l.setCurrency(skuData2.getCurrency());
                        });
                        continue;
                    }
                    purchaseList.stream().filter(l -> l.getSheetNumber().equals(bo.getSheetNumber())).forEach(l -> {
                        l.setPrice(0D);
                        l.setInvoiceDate(null);
                        l.setCurrency("");
                    });
                }
            }
        }
    }
}
